<?php
/**
 * @title 1626. 无矛盾的最佳球队
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $scores
     * @param Integer[] $ages
     * @return Integer
     */
    function bestTeamScore($scores, $ages) {
        /**
         * @since 2020-10-20 长度
         */
        $len = count($scores);

        /**
         * @since 2020-10-20 年龄分组
         */
        $datas = [];
        for($i=0; $i<$len; $i++){
            $datas[$ages[$i]][] = $scores[$i];
        }
        // return $datas;

        /**
         * @since 2020-10-20 结果集
         */
        $result = [];

        /**
         * @since 2020-10-20 年龄排序
         */
        $keys = array_keys($datas);
        sort($keys);

        /**
         * @since 2020-10-20 分数排序
         */
        foreach ($keys as $key){
            sort($datas[$key]);
            $result[] = $datas[$key];
        }
        // return $result;

        /**
         * @return
         */
        return self::find($result, 0);
    }

    /**
     * @title 无矛盾, 得分最大
     * @author start2004
     * @since 2020-10-21 Created
     *
     * @param array $result
     * @param int $min
     * @return float|int
     */
    public function find($result = [], $min = 0){
        if(count($result) == 1){
            $max = 0;
            $key = key($result);
            foreach ($result[$key] as $score){
                if($score >= $min){
                    $max += $score;
                } else {}
            }
        } else {
            /**
             * @since 2020-10-20 年龄最小组全不要
             */
            $data = $result;
            $key = key($data);
            unset($data[$key]);
            $max = self::find($data, $min);

            /**
             * @since 2020-10-20 年龄最小组, 得分大于等于min的
             */
            $data = [];
            $sum = 0;
            foreach($result[$key] as $score){
                if($score >= $min){
                    $data[] = $score;
                    $sum += $score;
                } else {}
            }
            unset($result[$key]);

            /**
             * @since 2020-10-21 年龄最小组全要, 依次去掉去高分
             */
            $lastScore = null;
            while( ($score = array_pop($data)) !== null){
                if($score === $lastScore){
                    $sum -= $score;
                    continue;
                } else {
                    $lastScore = $score;
                }

                /**
                 * @since 2020-10-21 年龄最小组得分
                 */
                $data[] = $score;
                $max = max($max, $sum+self::find($result, $score));
                // echo "data=[", implode(",", $data) ,"], sum={$sum}, score={$score}, max={$max}", PHP_EOL;

                /**
                 * @since 2020-10-21 抛出
                 */
                $sum -= $score;
                array_pop($data);
            }
        }

        /**
         * @return
         */
        return $max;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.18_3.php
 */
$datas = [
    // [[1,3,5,10,15], [1,2,3,4,5]], // 34
    // [[4,5,6,5], [2,1,2,1]], // 16
    // [[1,2,3,5], [8,9,10,1]], // 6
    [[1,3,7,3,2,4,10,7,5], [4,5,2,1,1,2,4,1,4]], // 29
    // [[564,915,436,927,784,205,186,992,518,467,264,180,528,594,557,462,667,856,104,911,960,176,382,96,153,685,359,370,623,480,213,180,881,333,658,964,367,261,758,822,790,904,246,441,97,938,202,434,88,24,881,147,439,260,47,27,39,79,751,758,493,950,94,224,769], [6,92,61,16,66,19,63,57,6,29,17,30,67,57,89,88,4,78,29,36,18,17,41,70,5,52,4,71,35,57,46,85,21,45,58,54,45,58,93,67,59,25,58,11,95,79,94,81,85,50,29,93,3,29,21,27,6,70,24,58,96,65,4,49,73]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->bestTeamScore(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
